{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "d2c6190b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['red', 'blue', 'green', 'black', 'white']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array = [\"red\", \"blue\", \"green\", \"black\", \"white\"]\n",
    "\n",
    "print(array)\n",
    "type(array)\n",
    "\n",
    "#python的数组可以存不同的类型\n",
    "array2 = [3, -200, 'hello']\n",
    "print(array2)\n",
    "type(array2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e8eb3466",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "red\n",
      "blue\n",
      "white\n"
     ]
    }
   ],
   "source": [
    "#基于下标获取元素\n",
    "\n",
    "#获取第一个元素\n",
    "print(array[0])\n",
    "\n",
    "#获取第二个元素\n",
    "print(array[1])\n",
    "\n",
    "#获取最后一个元素\n",
    "print(array[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4adca361",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['green', 'black']\n",
      "['black', 'white']\n",
      "['red', 'blue', 'green']\n",
      "['red', 'green', 'white']\n",
      "['red', 'black']\n"
     ]
    }
   ],
   "source": [
    "#数组的切片语法： 列表切片[起始下标：终止下标：步长]\n",
    "\n",
    "print(array[2:4])\n",
    "\n",
    "print(array[3:])\n",
    "\n",
    "print(array[:3])\n",
    "\n",
    "print(array[::2])\n",
    "\n",
    "print(array[::3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "aadce0c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"\\narray2.append('1')\\n\\n如果没有定义数组变量 直接调用append函数会抛出如下异常\\nNameError: name 'array2' is not defined\\n\""
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "# 添加和删除元素\n",
    "'''\n",
    "array2.append('1')\n",
    "\n",
    "如果没有定义数组变量 直接调用append函数会抛出如下异常\n",
    "NameError: name 'array2' is not defined\n",
    "'''\n",
    "\n",
    "array2 = [0,1,2,3,4,5]\n",
    "array2.append(6)\n",
    "print(array2)\n",
    "\n",
    "array2.pop()\n",
    "print(array2)\n",
    "\n",
    "array2.extend([6,7])\n",
    "print(array2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "42fc12d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1, 0]\n",
      "False\n",
      "[5, 4, 3, 2, 1, 0]\n"
     ]
    }
   ],
   "source": [
    "#反转数组的两种思路\n",
    "array3 = [0,1,2,3,4,5]\n",
    "\n",
    "print(array3[::-1])\n",
    "print(array3[::-1] == array3)\n",
    "\n",
    "array3.reverse()\n",
    "print(array3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a8a00b38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 5, 4, 3, 2, 1, 0]\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7]\n"
     ]
    }
   ],
   "source": [
    "#数组链接与重复\n",
    "print(array2 + array3)\n",
    "print(array2 * 2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
